<?php
$_SESSION['steps'][$_GET['action']] = false;
if($_SESSION['index']=='')
{
	displayError('Une erreur interne s\'est produit (perte du fichier index.xml)');
}

$ch="";
if(isset($_GET['post']) && $_GET['post']==1)
{
	$_SESSION['conf_db']['host'] = trim($_REQUEST['txt_host']);
	$_SESSION['conf_db']['database'] = trim($_REQUEST['txt_database']);
	$_SESSION['conf_db']['login'] = trim($_REQUEST['txt_login']);
	$_SESSION['conf_db']['password'] = trim($_REQUEST['txt_password']);
	$_SESSION['conf_db']['prefix'] = preg_replace('/[^a-zA-Z0-9_]/', '', $_REQUEST['txt_prefix']);
	
	if($_SESSION['conf_db']['host']=='')
	{
		$ch  .= '<span class="install_message_error">Le champs SQL Host est obligatoire...</span><br>';
	}
	if($_SESSION['conf_db']['login']=='')
	{
		$ch  .= '<span class="install_message_error">Le champs SQL Username est obligatoire...</span><br>';
	}
	if($_SESSION['conf_db']['database']=='')
	{
		$ch  .= '<span class="install_message_error">Le champs SQL Database est obligatoire...</span><br>';
	}
	if($ch=='')
	{
		$link = @mysql_connect($_SESSION['conf_db']['host'], $_SESSION['conf_db']['login'], $_SESSION['conf_db']['password']);
		if($link===false)
		{
			$ch  .= '<span class="install_message_error">Impossible de se connecter au server de données...<br><i>'.mysql_error().'</i></span>';
		}
		else 
		{
			if(!mysql_select_db($_SESSION['conf_db']['database'], $link))
			{
				$sql = 'CREATE database `'.$_SESSION['conf_db']['database'].'`';
				if(!mysql_query($sql, $link))
				{
					$ch  .= '<span class="install_message_error">Impossible de créer la base...<br><i>'.mysql_error().'</i></span>';
				}
				elseif(!mysql_select_db($_SESSION['conf_db']['database'], $link))
				{
					$ch  .= '<span class="install_message_error">Impossible de séléctionner la base...<br><i>'.mysql_error().'</i></span>';
				}
			}
		}
	}
	if($ch=='')
	{
		$_SESSION['steps'][$_GET['action']] = true;
?>
conf_mysql_next_fct()
<?php
		die();
	}
}
else
{
	if((!isset($_SESSION['conf_db']['database']) || $_SESSION['conf_db']['database']=='') && (!isset($_SESSION['conf_db']['host']) || $_SESSION['conf_db']['host']=''))
	{
		if(strpos($_SERVER['SERVER_ADDR'], '127.0.0.')!==false)
		{
			$_SESSION['conf_db']['host'] = 'localhost';
			$_SESSION['conf_db']['database'] = 'tt';
			$_SESSION['conf_db']['login'] = 'root';
		}
		elseif(strpos($_SERVER['SERVER_NAME'], '.free.fr')!==false)
		{
			$_SESSION['conf_db']['host'] = 'localhost';
			$_SESSION['conf_db']['database'] = substr($_SERVER['SERVER_NAME'], 0, -8);
			$_SESSION['conf_db']['login'] = $_SESSION['conf_db']['database'];
		}
		$_SESSION['conf_db']['password'] = '';
		$_SESSION['conf_db']['prefix'] = '';
		
	}
}

if($ch!='') $ch.='<br>';
$ch.=	'<form id="form_conf_mysql" action="javascript:document.getElementById(\'install_next\').onclick()"><label for="txt_host" class="form">SQL Host</label><input type="text" class="text" id="txt_host" name="txt_host" value="'.htmlspecialchars($_SESSION['conf_db']['host']).'"><br>
<label for="txt_database" class="form">SQL Database</label><input type="text" class="text" id="txt_database" name="txt_database" value="'.htmlspecialchars($_SESSION['conf_db']['database']).'"><br>
<label for="txt_login" class="form">SQL Username</label><input type="text" class="text" id="txt_login" name="txt_login" value="'.htmlspecialchars($_SESSION['conf_db']['login']).'"><br>
<label for="txt_password" class="form">SQL Password</label><input type="password" class="text" id="txt_password" name="txt_password" value="'.htmlspecialchars($_SESSION['conf_db']['password']).'"><br>
<label for="txt_prefix" class="form">SQL Prefix</label><input type="text" class="text" id="txt_prefix" name="txt_prefix" value="'.htmlspecialchars($_SESSION['conf_db']['prefix']).'">
<input type="image" src="./temp/setup_px.gif">
</form>
';


?>
document.getElementById('install_content_intro').innerHTML = 'Veuillez saisir les paramètres de connexion à la base de données. Si vous n\'êtes pas sur de vous, demandez a votre administrateur.';
document.getElementById('install_content_data').innerHTML = '<?php echo strToJs($ch)?>'
<?php
displayButtons();
?>

conf_mysql_next_fct = document.getElementById('install_next').onclick;
document.getElementById('install_next').onclick=function()
{
	document.getElementById('install_next').disabled = true;
	document.getElementById('install_back').disabled = true;
	
	document.getElementById('install_loading').style.display="block"

	ajax.sendAsync(page+'?action=conf_mysql&post=1&uid='+new Date().getTime(), function(datas)
	{
		document.getElementById('install_loading').style.display="none"
		document.getElementById('install_next').disabled = false;
		document.getElementById('install_back').disabled = false;
		evalAjax(datas)
	}, ajax.buildDatas(document.getElementById("form_conf_mysql")))
}